package jard.webshop.nbp;

import jard.webshop.util.FinancialException;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

/**
 *
 * @author cjp
 */
@Entity
public class Payment implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    // It seems reasonable to let people pay in installments
    //@ManyToOne(cascade={CascadeType.ALL})
    //But we'll wait with that
    //@JoinColumn(nullable=false)
    //@OneToOne
    //private Order order;
    @JoinColumn(nullable = false)
    @ManyToOne
    private Invoice invoice;
    @Column(nullable = false)
    private Float amount;

    public Payment() {
    }

    public Payment(Invoice invoice, Float amount) throws FinancialException {
        if (amount > 0) {
            this.invoice = invoice;
            this.amount = amount;
        } else {
            throw new FinancialException("Can't pay negative or zero amounts of money.");
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Payment)) {
            return false;
        }
        Payment other = (Payment) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "jard.webshop.nbp.Payment[ id=" + id + ", " + amount + " ]";
    }

    public Float getAmount() {
        return amount;
    }

    public void setAmount(Float amount) {
        this.amount = amount;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }
}
